package com.luoqy.speedy.aspect;

import com.alibaba.fastjson.JSON;
import com.luoqy.speedy.aspect.flow.entity.ExceptionLogParam;
import com.luoqy.speedy.aspect.flow.service.ExceptionLogService;
import com.luoqy.speedy.util.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @author admin
 */
@Aspect
@Component
public class ExceptionAspect {
    @Autowired
    ExceptionLogService exceptionLogService;
    @Pointcut("execution(* com.luoqy.speedy.*.*.controller.*.*(..))")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long time=0;
        long beginTime = System.currentTimeMillis();
        Result result = new Result();
        Object proceed=point.proceed();
        try{
            // 执行方法
            result = (Result) proceed;
            // 执行时长(毫秒)
            return result;
        }catch (Exception e){
            time = System.currentTimeMillis() - beginTime;
            saveLog(point, time,JSON.toJSONString(result),e);
            return proceed;
        }
    }
    @Async
    protected void saveLog(ProceedingJoinPoint joinPoint, long time,String result,Exception e) {
        ExceptionLogParam interfaceLog = new ExceptionLogParam();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        String methodClass=className+"."+methodName;
        interfaceLog.setReqPath(methodClass);
        interfaceLog.setReqMethod(methodClass);
        interfaceLog.setExecuteRemark("错误具体日志统一记录");
        interfaceLog.setExceptionRemark(JSON.toJSONString(e));
        /****************************参数*****************************************/
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Object[] values = joinPoint.getArgs();
            if(values.length==1){
                interfaceLog.setReqParams(JSON.toJSONString(values[0]));
            }else{
                String[] names = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
                for (int i = 0; i < names.length; i++) {
                    map.put(names[i], JSON.toJSONString(values[i]));
                }
                interfaceLog.setReqParams(map.toString());
            }
        }catch (Exception es){

        }


        /*********************************************************************/
        double times=time/1000.0;
        interfaceLog.setExecuteTime(times);
        interfaceLog.setResultParams(result);
        interfaceLog.setCreateTime(new Date());
        exceptionLogService.save(interfaceLog);
    }
}
